package es.ats.codington.festival.daos;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.ArrayList;

import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.interfaces.daos.IEventSignUpDAO;
import es.ats.codington.festival.utils.EventDbQuery;
import es.ats.codington.festival.utils.FERSDataConnection;
import es.ats.codington.festival.utils.JSONQuery;

/**
 * The Class EventSignUpDAOImpl.
 */
public class EventSignUpDAOImpl implements IEventSignUpDAO {
	
	/** The address. */
	private static String address = "src/es/ats/codington/festival/utils/DBJson.json";
	
	/** The Constant EventSignUp_Register. */
	private static final String EventSignUp_Register = "EventSignUp_Register";
 
	
	/**
	 * Register event by visitor.
	 *
	 * @param con the con
	 * @param idVisitor the id visitor
	 * @param idEvent the id event
	 * @return true, if successful
	 * @throws Exception the exception
	 * @see es.ats.codington.festival.interfaces.daos.IEventSignUpDAO#registerEventByVisitor(java.sql.Connection, int, int)
	 */
	@Override
	public boolean registerEventByVisitor(Connection con, int idVisitor, int idEvent) throws Exception{
		boolean rs = false;
		try{	
			
			/*JSONQuery reader = new JSONQuery(address);			
		    String query = reader.readString(EventSignUp_Register);*/
			String query = "INSERT INTO eventsignup (VisitorId, EventId) VALUES (?,?);";

		   // System.out.println("sql-> "+ query);
		  //  System.out.println("Visitor -> "+ idVisitor+ ". Event -> " + idEvent );
		    
		    rs = EventDbQuery.inserInEvent(idEvent, idVisitor, con, query);
		/*    
			if (rs) System.out.println("Event sign up correct");
				else System.out.println("Event sign up failed");*/
			
			con.close();
			
		}catch (Exception e){
			System.out.println(e.getMessage());
		}
		return rs;
	}

	
	/**
	 * Un register event in visitor.
	 *
	 * @param con the con
	 * @param idVisitor the id visitor
	 * @param idEvent the id event
	 * @return true, if successful
	 */
	public boolean unRegisterEventInVisitor(Connection con, int idVisitor, int idEvent) {
		boolean rs = false;
		try{	
			
			/*JSONQuery reader = new JSONQuery(address);			
		    String query = reader.readString(EventSignUp_Register);*/
			String query = "DELETE FROM eventsignup WHERE VisitorId=? AND EventId=?;";

		    System.out.println("sql-> "+ query);
		    System.out.println("Visitor -> "+ idVisitor+ ". Event -> " + idEvent );
		    
		    rs = EventDbQuery.deleteFromEvent(idEvent, idVisitor, con, query);
		    
			if (rs) System.out.println("Unregister correct");
				else System.out.println("Unregister failed");
			
			con.close();
			
		}catch (Exception e){
			System.out.println(e.getMessage());
		}
		return rs;
	}
	
	
	/*
	public int countEventByVisitor(Connection con, int idVisitor) {
		
		try{	
			
		JSONQuery reader = new JSONQuery(address);			
		    String query = reader.readString("SELECT * FROM eventsignup WHERE VisitorId=?;");
		    
		    String query = "SELECT * FROM eventsignup WHERE VisitorId=?;";
		    
		    int rs = EventDbQuery.countInEvent( idVisitor, con, query);
		    System.out.println("Eventos que tiene este visitor "+ rs);
			
			con.close();
			
		}catch (Exception e){
			System.out.println(e.getMessage());
		}		
		return rs;
	}
	*/
	
	
	/**
	 * Gets the events by visitor.
	 *
	 * @param con the con
	 * @param idVisitor the id visitor
	 * @param event the event
	 * @return the events by visitor
	 */
	public boolean getEventsByVisitor(Connection con, int idVisitor, ArrayList<Event> event) {
		boolean rs = false;
		try{	
			int res = 0;
			String query = "SELECT * FROM eventsignup WHERE VisitorId=?;";
		    res = EventDbQuery.countInEvent( idVisitor, con, query);
		    //get the idEvents
		    int[] events = new int [res]; 
		    res = EventDbQuery.countInEvent( idVisitor, con, query, events);
		    if (res==0){rs = false;} 
		    else {
		    	//Get the evets
		    	for(int i=0; i<events.length; i++){
		    		query = "SELECT * FROM event WHERE idEvent="+events[i]+";";
		    		rs = EventDbQuery.getAllEventsByOne(con, query, event, events[i]);
		    	}
		    }
		    
         System.out.println("Eventos que tiene este visitor "+ (events.length));	
			con.close();
			
		}catch (Exception e){
			System.out.println(e.getMessage());
		}		
		return rs;
	}
	
	
	/**
	 * Gets the semi all events.
	 *
	 * @param con the con
	 * @param idV the id v
	 * @param event2 the event2
	 * @return the semi all events
	 */
	public boolean getSemiAllEvents(Connection con, int idV, ArrayList<Event> event2){
			boolean rs = false;
			try{	
				int res = 0;
				String query = "SELECT * FROM event";
			    res = EventDbQuery.countEvents( idV, con, query);
			    //get the idEvents
			    int[] events = new int [res]; 
			    res = EventDbQuery.countnEvents( idV, con, query, events); 
			    if (res==0){rs = false;}
			    else {
			    	//Get the evets
			    	for(int i=0; i<events.length; i++){
			    		query = "SELECT * FROM event";// WHERE idEvent="+events[i]+";";
			    		rs = EventDbQuery.getAllEventsByOne(con, query, event2, events[i], i);
			    	}
			    }
			    
	    //     System.out.println("Eventos que le faltan a este visitor "+ (events.length));	
				con.close();
				
			}catch (Exception e){
				System.out.println(e.getMessage());
			}		
			return rs;
		}
	
	/**
	 * Insert event dao.
	 *
	 * @param con the con
	 * @param event the event
	 * @return true, if successful
	 */
	public boolean insertEventDAO(Connection con, Event event) {
		// TODO Auto-generated method stub
		return false;
	}
 
	/**
	 * Register event by visitor.
	 *
	 * @param con the con
	 * @param idVisitor the id visitor
	 * @param idEvent the id event
	 * @throws Exception the exception
	 */
	public void RegisterEventByVisitor(Connection con, int idVisitor,
			int idEvent) throws Exception {
		// TODO Auto-generated method stub
		
	}
}